<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>
  
  <script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
    dataPropos: Array,
    text: String,
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");

      this.chart.setOption({
        title: {
          text: this.text,
          left: "center",
          textStyle: {
            fontSize: 16,
            color: "#999",
          },
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)",
        },
        legend: {
          orient: "vertical",
          left: "left",
          padding: [0, 0, 0, 10],
          data: this.dataPropos.map((item) => item["name"]),
        },
        series: [
          {
            name: "用户类型",
            type: "pie",
            radius: "60%",
            center: ["56%", "50%"],
            data: this.dataPropos,
            itemStyle: {
              normal: {
                color: function (colors) {
                  var colorList = [
                    "#19be6b",
                    "#ff9900",
                    "#ed3f14",
                    "#e46cbb",
                    "#9a66e4",
                    "#8ec9eb",
                    "#8b0a50",
                  ];
                  return colorList[colors.dataIndex];
                },
              },
            },
            animationEasing: "cubicInOut",
            animationDuration: 2600,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      });
    },
  },
};
</script>
  ../../../../travel-ui/src/views/dashboard/mixins/resize